package com.resource.fxLoader
{
	import flash.display.DisplayObject;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	[Event(name="cancel", type="flash.events.Event")]
	[Event(name="complete", type="flash.events.Event")]
	[Event(name="progress", type="flash.events.ProgressEvent")]
	internal class InternalLoader extends EventDispatcher
	{
		private var _isLoading:Boolean;
		
		private var _url:String;
		
		private var _urlLoader:URLLoader;
		
		public var data:ByteArray;
		
		public function InternalLoader()
		{
			_urlLoader = new URLLoader();
			_urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
		}
		
		
		public function loadResource(key:String):void
		{
			_url = key;
			var urlReq:URLRequest;
			
			urlReq = new URLRequest(_url);
			addListeners();
			_urlLoader.load(urlReq);
			_isLoading = true;
		}

		
		private function onLoadComplete(e:Event):void
		{
			removeListeners();
			
			data = e.target.data as ByteArray;
			
		
			_isLoading = false;
			this.dispatchEvent(e);
		}
		
		
		private function onLoadError(e:ErrorEvent):void
		{
			switch(e.type)
			{
				case IOErrorEvent.IO_ERROR:
					trace((e as IOErrorEvent).toString(),2);
					//TraceLogger.log((event as IOErrorEvent).toString(),2);
					break;
				//case other
			}
		
			removeListeners();
		
			_isLoading=false;
			dispatchEvent(new Event(Event.CANCEL));
		}
		
		
		private function onLoadingProgress(e:ProgressEvent):void
		{
			
		}
		
		
		private function addListeners():void
		{
			_urlLoader.addEventListener(Event.COMPLETE,onLoadComplete);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onLoadError);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
			_urlLoader.addEventListener(Event.CANCEL,onLoadError);
			_urlLoader.addEventListener(ProgressEvent.PROGRESS, onLoadingProgress);
		}
		
		
		private function removeListeners():void
		{
			_urlLoader.removeEventListener(Event.COMPLETE, onLoadComplete);
			_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,onLoadError);
			_urlLoader.removeEventListener(Event.CANCEL,onLoadError);
			_urlLoader.removeEventListener(ProgressEvent.PROGRESS, onLoadingProgress);
		}
		
		
		
		
		public function get url():String
		{
			return _url;
		}

		public function set url(value:String):void
		{
			_url = value;
		}

		
		public function get isLoading():Boolean
		{
			return _isLoading;
		}
		 

		public function get progress():Number
		{
			return _urlLoader.bytesLoaded/_urlLoader.bytesTotal;
		}
	}
}